# 新建一个Dict，写入每个x，y
# load 针对文件句柄 loads 针对内存对象
# shapes 字典数组，所有的数据整合在其中
# 读取数据 -> 新建字典 -> 加入字典集合
# 格式化问题，cpickle和pickle使用

import json
from decimal import *



num = 0
with open(r'C:\Users\Admin\Desktop\Qiu破裂_FeaturesToJSON.json', 'r', encoding='utf-8') as f:

    line = f.readline()
    d = json.loads(line)
    hhj=0
    for i in range(0, 11):
        for j in range(0, 16):
            dic = {
                'version': '4.5.7',
                'flags': {},
                'shapes': []
            }
            num += 1
            hhj=0
            with open(r'C:\Users\Admin\Desktop\imagedata\retif\\'+str(i)+'_'+str(j)+'.json','r',encoding='utf-8') as fp:
                fp_line = fp.readlines()
                temp1 = eval("{"+str(fp_line[17])+"}")
                temp2 = eval("{"+str(fp_line[18])+"}")
                count = 0
                d_f = d['features']
                while True:
                    initial_dict = {
                        'label': 'line',
                        'points': [],
                        'group_id': None,
                        'shape_type': 'linestrip',
                        'flags': {}
                    }
                    x_min = 98.0046670338 + j * 480 * 2.83574e-07 - 124 * 2.83574e-07 # float
                    x_max = x_min + 480 * 2.83574e-07
                    y_min = 34.6951523581 + (10 - i) * 360 * 2.83574e-07 + 63 * 2.83574e-07
                    y_max = y_min + 360 * 2.83574e-07  # float
                    count += 1
                    try:
                        d_f_a=d_f[count-1]['attributes']['FID']
                    except:
                        break
                    for x_y in d_f[count-1]['geometry']['paths'][0]:
                        x = float(x_y[0])
                        y = float(x_y[1])
                        if (x > x_min and x < x_max) and (y > y_min and y < y_max):
                            x = int((x - 98) * 1e14) - int((x_min - 98) * 1e14)
                            x = (x // 283574 + 1) / 100
                            y = int((y - 34) * 1e14) - int((y_min - 34) * 1e14)
                            y = 360 - (y // 283574 + 1) / 100
                            x_y = [x, y]
                            initial_dict['points'].append(x_y)
                            dic['shapes'].append(initial_dict)
                            hhj += 1
                    print(i,j)
                    print(hhj)
                dic['imagePath'] = temp1['imagePath']
                dic['imageData'] = temp2['imageData']
                dic['imageHeight'] = 360
                dic['imageWidth'] = 480

            json.dump(dic, open(r'C:\Users\Admin\Desktop\imagedata\reline/'+str(i)+'_'+str(j)+'.json', 'w', encoding='utf-8'), indent=4)
